package com.JZ.QM.controller;

import com.JZ.QM.common.entity.ProtocolRecordEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.service.ProtocolRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/7/6 22:45
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/protocol")
public class ProtocolController {

    @Resource
    ProtocolRecordService protocolRecordService;

    /**
     * 记录用户签署协议
     * @param comBaseId
     * @param protocolId  1 为风险提示协议   2 为组合用户服务协议
     * @param userId
     * @return
     */
    @RequestMapping("/protocolRecord")
    public CommonResult protocolRecord(Long comBaseId, Long protocolId, Long userId){
        //协议流水号
        String logicId = "";
        //协议实体类
        ProtocolRecordEntity protocolRecordEntity = new ProtocolRecordEntity();
        //保存协议flag
        boolean saveFlag = false;

        //进行协议流水号的组装
        if(protocolId == 1){
            String timeUUid = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            logicId = "PR" + timeUUid;
        }else {
            String timeUUid = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            logicId = "PU" + timeUUid;
        }

        protocolRecordEntity.setComBaseId(comBaseId);
        protocolRecordEntity.setLogicId(logicId);
        protocolRecordEntity.setRecordStatus(1);
        protocolRecordEntity.setUserId(userId);
        protocolRecordEntity.setProtocolId(protocolId);

        saveFlag = protocolRecordService.save(protocolRecordEntity);

        if(saveFlag){
            return CommonResult.success("协议签署成功");
        }else {
            return CommonResult.error("协议签署失败");
        }
    }
}
